package com.example.kaoshi;

import java.util.Scanner;

/**
 * Created by Quincy on 2018/9/9.
 */
public class Test8 {
    static int[][] rect = null;

    public static void main(String[] args){
        findDept();

    }

    public static void findDept(){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        rect =new int[n][n];
        if (rect.length > 1000 || rect.length <= 1)
            return;
        for (int i = 0; i < n ; i++){
            for (int j = 0; j < n; j++)
                rect[i][j] = in.nextInt();
        }
        int count = 0;
        // 遍历矩阵找1,块的定位
        for (int i = 0; i < rect.length; i++) {
            for (int j = 0; j < rect[i].length; j++) {
                // 当找到1时,开始处理其所在的块
                if (rect[i][j] == 1) {
                    if (i <= 0 || j <= 0 || i >= 1000 || j >= 1000)
                        break;
                    block(i, j);
                    count++;
                }
            }
        }

        System.out.println("该矩阵中,共有" + count + "块");

    }

    private static void block(int i, int j) {

        // 修改(i,j)坐标对应的数组元素的值(避免递归时反复判断相邻元素)
        rect[i][j] = 4;
        // 分别判断上下左右
        if (i < rect.length - 1 && rect[i + 1][j] == 1) {
            block(i + 1, j);
        }
        if (i > 0 && rect[i - 1][j] == 1) {
            block(i - 1, j);
        }
        if (j < rect[i].length - 1 && rect[i][j + 1] == 1) {
            block(i, j + 1);
        }
        if (j > 0 && rect[i][j - 1] == 1) {
            block(i, j - 1);
        }
    }



}
